var list = {};

list.param = {
	page:1,
	rows:6,
	total:10,
	uid:0,
	flag:null
};


list.getHeight = function () {
    var height = $(document).height();
    window.parent.index.iframeResize(height);
}

list.toCategory = function() {
	$('.send_spread').click(function(){
		window.location = "spread_category.html";
	});
}

list.pageBack = function(num, type) {
	list.param.page = num;
	list.getUserIdAndData();
}

list.pageInit = function(pageNum, page, pageChangeBack) {
	$('#pagination').empty();
    var jqPaginator = $.jqPaginator('#pagination', {
        totalPages: pageNum,
        visiblePages: 5,
        currentPage: page,
        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
        //onPageChange: function (num, type) {
         //   $('#p1').text(type + '：' + num);
        //}
        onPageChange: pageChangeBack
    });
}

list.getUserIdAndData = function () {
	var param = new common.GetParameters();
	list.param.uid = param.userid;
	var req = new common.Request();
	var url = "orderInfo/findAllByUId.action";
	var reqData = {};
	for (var key in list.param) {
		if (list.param[key] != null) {
			reqData[key] = list.param[key];
		}
	}
	req.sendMethodRequest("get", url, reqData, function(e) {
		console.log(e);
		var spread_list = $('#spread_list');
		if (e != null && e.isSuccess) {
			var listEle = e.orderInfoList;
			spread_list.empty();
			for (var i = 0; i < listEle.length; i ++) {
				(function(i){
					var trEle = $('<tr></tr>').attr('dataId', listEle[i].id).attr('flag', listEle[i].flag).appendTo(spread_list).click(function(){
						var id = $(this).attr('dataId');
						var flag = $(this).attr('flag');
						window.location = "status_info.html?id=" + id + "&flag=" + flag;
					});
					var nameTd = $('<td></td>').attr('width', "20%").appendTo(trEle);
					var nameS = $('<p></p>').attr('title', listEle[i].name).html(common.substr(listEle[i].name, 0, 6)).appendTo(nameTd);
					
					var time1 = new Date(listEle[i].createTime);
					var timeStr = time1.getFullYear()+"-"+(time1.getMonth()+1)+"-"+time1.getDate();
					var timeTd = $('<td></td>').attr('width', "15%").html(timeStr).appendTo(trEle);
					
					var numTd = $('<td></td>').attr('width', "20%").appendTo(trEle);
					var numS =  $('<p></p>').attr('title', listEle[i].num).html(common.substr(listEle[i].num, 0, 10)).appendTo(numTd);
					
					var priceTd = $('<td></td>').attr('width', "15%").html((listEle[i].price == null ? 0 : listEle[i].price) + "元").appendTo(trEle);
					var statusTd = $('<td></td>').attr('width', "15%").html("已提交").appendTo(trEle);
					var opTd = $('<td></td>').attr('width', "15%").appendTo(trEle);
					if (listEle[i].status == 1) {
						var aEle = $('<a></a>').attr('dataId', listEle[i].id).attr('flag', listEle[i].flag).html("修改").appendTo(opTd).click(function(){
							var id = $(this).attr('dataId');
							var flag = $(this).attr('flag');
							if (flag == 1) {
								window.location = "spread_write.html?id=" + id + "&type=update";
							} else if (flag == 2) {
								window.location = "spread_draw.html?id=" + id + "&type=update";
							} else if (flag == 3) {
								window.location = "spread_send.html?id=" + id + "&type=update";
							} else {
								window.location = "spread_covet.html?id=" + id + "&type=update";
							}
							event.stopPropagation();
						});
					} else {
						var aEle = $('<a></a>').appendTo(opTd);
					}
					
					var emptyEle = $('<tr></tr>').appendTo(spread_list);
					for (var j = 0; j < 6; j ++) {
						$('<td></td>').appendTo(emptyEle);
					}
				})(i);
			}
			var init = false;
			list.pageInit(e.sum, list.param.page, function(num, type){
				list.param.page = num;
				if (init) {
					list.getUserIdAndData();
				} else {
					init = true;
				}
			});
			
		}
	}, function(e){
		
	});
}

list.itemsInit = function() {
	var lis = $('.con .tent .menu .items li');
	for (var i = 0; i < lis.length; i ++) {
		(function(i){
			var liEle = $(lis[i]).click(function(){
				var liArray = $('.con .tent .menu .items li');
				for (var j = 0; j < liArray.length; j ++) {
					$(liArray[j]).removeClass('select');
				}
				$(this).addClass('select');
				if ($(this).val() == "all") {
					list.param.flag = null;
				} else {
					list.param.flag = $(this).val();
				}
				list.param.page = 1;
				list.getUserIdAndData();
			});
		})(i);
	}
}

list.init = function() {
	try {
		list.getHeight();
	} catch (e) {
		
	}
	list.getUserIdAndData();
	list.toCategory();
	list.itemsInit();
}

$(document).ready(function() {
	list.init();
});